// Usage: benchmark bm([](){ /* some codes */ })
//        bm.run();
//        cout << bm.duration() << " seconds";

#include <functional>
#include <sys/time.h>

class benchmark
{
	timeval start;
	timeval stop;
public:
	std::function<void()> func;
	benchmark(std::function<void()> func) : func(func) {}
	void run() {
		gettimeofday(&start, NULL);
		func();
		gettimeofday(&stop, NULL);
	}
	double duration() {
		long secs(stop.tv_sec - start.tv_sec);
		int usecs(stop.tv_usec - start.tv_usec);
		
		if(usecs < 0)
		{
			--secs;
			usecs += 1000000;
		}
		
		return (usecs / 1000000.0 + secs);
	}
};
